﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;

namespace Sendz.Domain
{
    public class AutoResponder
    {
        // Entity
        [HiddenInput(DisplayValue = false)]
        public Guid AutoResponderKey { get; set; }

        [DisplayName("AutoResponder Name")]
        [Required(ErrorMessage = "A name for your autoresponder is required")]
        [StringLength(50, ErrorMessage = "Please ensure that the autoresponder name you enter is less than 50 characters")]
        public string Name { get; set; }

        [DisplayName("Email From Name")]
        [Required(ErrorMessage = "An email from name is required")]
        [StringLength(100, ErrorMessage = "Please ensure that the email from name you enter is less than 100 characters")]       
        public string FromName { get; set; }

        [DisplayName("Email From Address")]
        [Required(ErrorMessage = "An email from address is required")]
        [RegularExpression(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", ErrorMessage = "Please enter a valid email from address")]
        [StringLength(100, ErrorMessage = "Please ensure that the email from address you enter is less than 100 characters")]       
        public string FromEmail { get; set; }

        [DisplayName("Email Subject")]
        [Required(ErrorMessage = "An email subject is required")]
        [StringLength(255, ErrorMessage = "Please ensure that the email subject you enter is less than 255 characters")]     
        public string Subject { get; set; }

        [DisplayName("Email Body")]
        [Required(ErrorMessage = "An email body is required")]
        [DataType(DataType.MultilineText)]
        public string Body { get; set; }

        [HiddenInput(DisplayValue = false)]
        public bool IsRemoved { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime LastModified { get; set; }

        [HiddenInput(DisplayValue = false)]
        public DateTime CreatedOn { get; set; }

        // Parents
        [HiddenInput(DisplayValue = false)]
        public Guid UserKey { get; set; }
        public User User { get; set; }

        // Children
        [HiddenInput(DisplayValue = false)]
        public List<Form> Forms { get; set; }

        [HiddenInput(DisplayValue = false)]
        public List<AutoResponse> AutoResponses { get; set; }

        [HiddenInput(DisplayValue = false)]
        public List<Attachment> Attachments { get; set; }

        // Constructors
        public AutoResponder()
        {
            this.AutoResponderKey = Guid.NewGuid();
            this.CreatedOn = DateTime.Now;
            this.Attachments = new List<Attachment>();
        }

        public AutoResponder(Guid userKey)
        {
            this.AutoResponderKey = Guid.NewGuid();
            this.UserKey = userKey;
            this.CreatedOn = DateTime.Now;
            this.Attachments = new List<Attachment>();
        }

        public AutoResponder(Guid userKey, string name, string fromName, string fromEmail, string subject, string body)
        {
            this.AutoResponderKey = Guid.NewGuid();
            this.UserKey = userKey;
            this.Name = name;
            this.FromName = fromName;
            this.FromEmail = fromEmail;
            this.Subject = subject;
            this.Body = body;
            this.CreatedOn = DateTime.Now;
            this.Attachments = new List<Attachment>();
        }
    }
}
